<style>
  .picImg {
    width: 40px;
    height: 40px;
  }
</style>
<template>
  <div class="app-container">
    <!--&lt;!&ndash; 查询和其他操作 &ndash;&gt;-->
    <div class="filter-container">
      <el-form :inline="true" status-icon label-position="left" class="demo-form-inline">
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker v-model="listQuery.startTime" type="datetime" placeholder="开始时间"
                          value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="listQuery.endTime" type="datetime" placeholder="结束时间"
                          value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>
        </el-form-item>
        <el-form-item label="是否已读" prop="isRead">
          <el-select v-model="listQuery.isRead" placeholder="请选择">
            <el-option label="未读" value="0"></el-option>
            <el-option label="已读" value="1"></el-option>
            <el-option label="全部" value="9"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </div>


    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row
              :header-cell-style="{background:'#92D3FF',color:'#fff'}">

      <el-table-column align="center" min-width="100" label="发送用户名称" prop="sendUser"/>
      <el-table-column align="center" min-width="100" label="接收用户名称" prop="recvUser"/>
      <el-table-column align="center" min-width="100" label="发送内容" prop="content"/>
      <el-table-column align="center" min-width="100" label="等级" prop="level">
        <template slot-scope="scope">
          <span>{{scope.row.level | levelFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" min-width="100" label="发送时间" prop="timeSend"/>
      <el-table-column align="center" min-width="100" label="是否已读" prop="isRead">
        <template slot-scope="scope">
          <span>{{scope.row.isRead | isReadFilter }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" min-width="100" label="对应的操作或跳转URL地址" prop="url"/> -->
      <el-table-column align="center" label="操作" width="270" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-popover
            placement="top"
            width="200"
            trigger="manual"
            :ref="'popover-' + scope.row.sendID">
            <p>确定设为已读吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.sendID].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click="doRead(scope.row)">确定</el-button>
            </div>
            <el-button v-if="!scope.row.isRead" type="danger" size="mini" slot="reference"
                       @click="$refs[`popover-` + scope.row.sendID].doShow()">设为已读
            </el-button>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
  import { doGet, doPost } from '@/api'
  import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
  import checkPermission from '@/utils/permission' // 权限判断函数
  export default {
    name: 'bank',
    components: { Pagination },
    filters: {
      levelFilter(level) {
        return { '0': '信息', '1': '留言', '2': '通知', '3': '加急', '4': '紧急' }[level]
      },
      isReadFilter(isRead) {
        return { '0': '未读', '1': '已读' }[isRead]
      }
    },
    data() {
      return {
        list: [],
        total: 0,
        listLoading: true,
        pickerOptions1: {
          disabledDate(time) {
            return time.getTime() > Date.now()
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date())
            }
          }]
        },
        listQuery: {
          pageNum: 1,
          pageSize: 20,
          startTime: undefined,
          endTime: undefined,
          isRead: '9'
        },
        editForm: {},
        editFormRules: {}
        // editDialogVisible: false
      }
    },
    created() {
      this.getList()
    },
    methods: {
      checkPermission,
      getQuery(){
        this.listQuery.pageNum=1;
        this.listQuery.pageSize=20;
        this.getList()
      },
      getList() {
        this.listLoading = true
        doGet('inmsg.html?op=list', this.listQuery).then(response => {
          this.list = response.rows
          this.total = response.data.totalcount
          this.listLoading = false
        }).catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      },
      doEdit() {
        this.$refs['editForm'].validate(valid => {
          if (valid) {
            doGet('inmsg.html?op=list', this.editForm)
              .then(response => {
                this.list = response.rows
                this.total = response.rows.length
                this.editDialogVisible = false
                this.listLoading = false
                this.$notify.success({
                  title: '成功',
                  message: '查询成功'
                })
              })
              .catch(response => {
                this.$notify.error({
                  title: '失败',
                  message: response.message
                })
              })
          }
        })
      },
      // inmsg.html?op=setread
      doRead(row) {
        doGet('inmsg.html?op=setread',{sendID:row.sendID} )
          .then(response => {
            this.$refs[`popover-` + row.sendID].doClose()
            let index=this.list.indexOf(row)
            this.list[index].isRead=1
            this.$notify.success({
              title: '成功',
              message: '设置已读成功'
            })
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.message
            })
          })
      }
    }
  }
</script>
